/*
 * irc.h
 *
 *  Created on: Mar 16, 2013
 *      Author: kasa
 */

#ifndef IRC_H_
#define IRC_H_

#define IRCMAXSZ 512

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>

#include <string.h>
#include <time.h>
#include <errno.h>


//char ircbuffer[IRCMAXSZ*2];

typedef struct {
	char * prefix;
	char * command;
	char * params;
} IRCMessage;

typedef struct {
	int socket;
	char *host;
	char *port;
	int cleanbuf;
//	char recvbuf[IRCMAXSZ]; /* move these somewhere else */
	char buffer[IRCMAXSZ*2];/* move this too */
	IRCMessage *lastmsg;
	struct addrinfo *res;
} ServerInfo;

typedef struct {
	char *nick;
	char *ident;
	char *realname;
} ClientInfo;

struct node {
	struct node * parent;
	struct node * left;
	struct node * right;
	char * command;
	void (*handler)(ServerInfo *, char *);
};
typedef struct node Node;

int addNode(Node *root, Node *leaf);
int delNode(Node *root, Node *leaf);
Node * findNode(Node *root, char *key);

int irc_connect(ServerInfo *irchost);
int irc_register(ServerInfo *irchost, ClientInfo *ircuser);
int irc_msg_default(ServerInfo *irchost, char *msg);
//extern int irc_msg_handler(ServerInfo *irchost, Node *root, char *command);
int irc_send(ServerInfo *irchost, char *msg);
IRCMessage * irc_parsemsg(char *raw);

/* currently handlers get (ServerInfo*, char *) as arguments */
int irc_handler_add(Node *root, char *command, void *fp);
int irc_handler_del(Node *root, char *command);
void * irc_handler_get(Node *root, char *command);

void * irc_getMessage(ServerInfo *irc);
char * irc_raw(ServerInfo *irc);
#endif /* IRC_H_ */
